#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include <sys/wait.h> 
using namespace std;
#define BUFFER_SIZE 16
int main()
{   
    const char * fileName = "./commonFifo";

    int ret = access(fileName,F_OK);
    if(ret == -1)
    {
        ret = mkfifo(fileName,0644);
        if(ret != 0)
        {
        perror("mkfifo error:");
        exit(-1);
        }
    }
    int fd = open(fileName,O_RDWR);
    if(fd == -1)
    {
        perror("open error:");
        _exit(-1);
    }
    pid_t pid = fork();
    if(pid > 0)
    {
        printf("parent process\n");

        int fd = open(fileName,O_WRONLY);
        if(fd == -1)
        {
            perror("open error:");
            _exit(-1);
        }

        int cnt = 3;
        const char *buf = "hello";

        int writeBytes = 0;
        while(cnt--)
        {   
            sleep(2);
            writeBytes = write(fd,buf,strlen(buf));
            if(writeBytes > 0)
            {
                cout<<"writeBytes:"<<writeBytes<<endl;
            }
            else if(writeBytes = 0)
            {
                cout<<"writeBytes == "<<endl;
            }
            else if(writeBytes < 0)
            {
                perror("write error:");
            }
        }
        close(fd);
        // wait(NULL);
    }
}